package cn.edu.zafu.bigdata.LiJuan;

public class Cube {
    // 属性：长、宽、高
    private final double length;
    private final double width;
    private final double height;

    // 构造方法
    public Cube(double length, double width, double height) {
        this.length = length;
        this.width = width;
        this.height = height;
    }

    // 获取长
    public double getLength() {
        return length;
    }

    // 获取宽
    public double getWidth() {
        return width;
    }

    // 获取高
    public double getHeight() {
        return height;
    }

    // 重写equals方法
    @Override
    public boolean equals(Object obj) {
        // 检查是否为同一对象
        if (this == obj) {
            return true;
        }
        // 检查是否为空或类型不同
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        // 类型转换
        Cube other = (Cube) obj;
        // 比较长、宽、高是否相等
        return Double.compare(other.length, length) == 0 &&
                Double.compare(other.width, width) == 0 &&
                Double.compare(other.height, height) == 0;
    }

    // 重写hashCode方法
    @Override
    public int hashCode() {
        // 使用一个质数作为乘数，与equals方法保持一致
        int result;
        long temp;
        temp = Double.doubleToLongBits(length);
        result = (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(width);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(height);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    // 测试用例
    public static void main(String[] args) {
        Cube cube1 = new Cube(1.0, 3.0, 5.0);
        Cube cube2 = new Cube(1.0, 3.0, 5.0);
        Cube cube3 = new Cube(5.0, 6.0, 7.0);

        System.out.println(" cube1的长宽高" + cube1.getLength()+" " + cube1.getWidth() +" "+ cube1.getHeight());
        System.out.println(" cube2的长宽高" + cube2.getLength() +" "+ cube2.getWidth() +" "+ cube2.getHeight());
        System.out.println(" cube3的长宽高" + cube3.getLength() +" "+ cube3.getWidth() +" "+ cube3.getHeight());

        // 测试相等性
        System.out.println("cube1 equals cube2: " + cube1.equals(cube2)); // 应该输出true
        System.out.println("cube1 equals cube3: " + cube1.equals(cube3)); // 应该输出false
    }
}
